/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package EntityPackage;

import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author work
 */
@Stateless
@LocalBean
public class BusinessLogic {
    @PersistenceContext(unitName = "csm-catering-ejbPU")
    private EntityManager em;
    @EJB
    private PrenotazioneFacadeLocal prenotazioneFacade;
    @EJB
    private ClienteFacadeLocal clienteFacade;
    @EJB
    private MenuFacadeLocal menuFacade;
    @EJB
    private LocationFacadeLocal locationFacade;
    @EJB
    private AgenziaFacadeLocal agenziaFacade;
    @EJB
    private PiattoFacadeLocal piattoFacade;
    

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    
    public boolean addCliente(Cliente c) {
        try{
        clienteFacade.create(c);
        }catch(Exception e){return false;};
        return true;
    }
    
    public boolean addPrenotazione(Prenotazione prenotazione) {
        try{
        prenotazioneFacade.create(prenotazione);
        }catch(Exception e){return false;};
        return true;
    }
    
    public boolean addMenu(Menu menu) {
        try{
        menuFacade.create(menu);
        }catch(Exception e){return false;};
        return true;
    }

    public Cliente verifyClient(String user, String pwd1) {
        Query getPwd = em.createNamedQuery("Cliente.findByUsername");
        getPwd.setParameter("username", user);
        Cliente cl;
        try{
            cl = (Cliente) getPwd.getSingleResult();
        }catch(NoResultException e){return null;}
        if(cl.getPassword().equals(pwd1))
            return cl;
        else return null;
    }

    public boolean usedUsername(String user) {
        Query getClient = em.createNamedQuery("Cliente.findByUsername");
        getClient.setParameter("username", user);
        Object Client;
        try{
            Client = getClient.getSingleResult();
        }catch(NoResultException e){return false;}
        return true;
    }
    
    public Cliente getClienteByMail(String mail) {
        Query getClient = em.createNamedQuery("Cliente.findByEmail");
        getClient.setParameter("email", mail);
        Cliente cl;
        try{
            cl = (Cliente) getClient.getSingleResult();
        }catch(NoResultException e){return null;}
        return cl;
    }
    
    public Cliente findClientById(int id) {
        Cliente cl;
        try{
            cl = (Cliente) clienteFacade.find(id);
        }catch(NoResultException e){return null;}
        return cl;
    }
    
    public Location findLocationById(int id) {
        Location loc;
        try{
            loc = (Location) locationFacade.find(id);
        }catch(NoResultException e){return null;}
        return loc;
    }
    
    public Menu findMenuById(int id) {
        Menu m;
        try{
            m = (Menu) menuFacade.find(id);
        }catch(NoResultException e){return null;}
        return m;
    }
    
    public Agenzia findAgenziaById(int id) {
        Agenzia a;
        try{
            a = (Agenzia) agenziaFacade.find(id);
        }catch(NoResultException e){return null;}
        return a;
    }
    
    public List<Agenzia> findAllAgenzia() {
        List<Agenzia> p;
        try{
            p = (List<Agenzia>) agenziaFacade.findAll();
        }catch(Exception e){return null;}
        return p;
    }
    
    public List<Piatto> findAllPiatto() {
        List<Piatto> p;
        try{
            p = (List<Piatto>) piattoFacade.findAll();
        }catch(Exception e){return null;}
        return p;
    }
    
  /*public String getLocationAddress(int id){
        Query getAddress = em.createNamedQuery("Location.getAddress");
        getAddress.setParameter("idLocation", id);
        String address;
        try {
            address = (String) getAddress.getSingleResult();
        }catch(NoResultException e){return null;}
        return address;
    }
    
    public String getLocationCity(int id) {
        Query getCity = em.createNamedQuery("Location.getCitta");
        getCity.setParameter("idLocation", id);
        String città;
        try {
            città = (String) getCity.getSingleResult();
        }catch(NoResultException e){return null;}
        return città;
    }
    
    public String getAgenziaName(int id){
        Query getName = em.createNamedQuery("Agenzia.getNome");
        getName.setParameter("idAgenzia", id);
        String name;
        try {
            name = (String) getName.getSingleResult();
        }catch(NoResultException e){return null;}
        return name;
    }
    
    public Collection<Menu> getPiatti(int id) {
        Query getRes = em.createNamedQuery("Menu.findByIdMenu");
        getRes.setParameter("idMenu", id);
        Collection<Menu> piatti;
        try {
            piatti = (Collection<Menu>) getRes.getResultList();
        }catch(NoResultException e){return null;}
        return piatti;
    }
    
    public String getNomePiatto(int id) {
        Query getName = em.createNamedQuery("Piatto.getNome");
        getName.setParameter("idPiatto", id);
        String name;
        try {
            name = (String) getName.getSingleResult();
        }catch(NoResultException e){return null;}
        return name;
    }   */
    
    public void persist(Object object) {
        em.persist(object);
    }

}
